package com.digitaldreamers.framework.network.packet;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * Abstract class for all receivable packet classes.
 *
 * @author DaoLe
 */
public abstract class AbReceivablePacket {

    // Packet ID.
    protected int mPacketId;

    /**
     * Read packet data.
     *
     * @param data The data sent from client.
     * @throws IOException Error that occurred while trying to parse data sent
     * from client.
     */
    public void fromByteArray(byte[] pData) throws IOException {
        ByteArrayInputStream BAIS = new ByteArrayInputStream(pData);
        DataInputStream DIS = new DataInputStream(BAIS);
        try {
            readData(DIS);
        } catch (IOException ex) {
            throw ex;
        } finally {
            DIS.close();
        }
    }
    
    /**
     * Read packet data.
     * @param DIS Input stream.
     * @throws IOException Error that occurred while reading data sent
     */
    protected abstract void readData(DataInputStream pDIS) throws IOException;
}